<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class ChannelController extends AdminbaseController {
	
	function _initialize() {
		parent::_initialize();
	}
	
	function index(){
		$status = I("post.status", -1);
		$name = I("post.name", "");
		$result = $this->curl_http(C('video_url').'channel/getList?token='.$_SESSION["TOKEN"], 'status='.$status.'&channelName='.$name);
		$this->assign("data", $result['ReturnData']);
		$this->assign("formParam", array('status'=>$status,'name'=>$name));
		$this->display();
	}
	
	function add(){
		$this->display();
	}
	
	function add_post(){
		if(IS_POST){
			$channelName = I("post.channelName", '');
			if(empty($channelName)){
				return $this->error("机位名称不能为空！");
			}
			
			$status = I("post.status", 1);
			$manager = I("post.manager", NULL);
			$managerPhone = I("post.managerPhone", NULL);
			$channelDescribe = I("post.channelDescribe", NULL);
			$deviceDescribe = I("post.deviceDescribe", NULL);
			
			$config = array('SecretId'       => C("API_SecretId"),
					'SecretKey'      => C("API_SecretKey"),
					'RequestMethod'  => 'GET',
					'DefaultRegion'  => C("API_DefaultRegion"));

			vendor("QcloudApi.QcloudApi");
			$service = \QcloudApi::load(\QcloudApi::MODULE_LIVE, $config);
			$package = array('channelName' => $channelName,
                 'channelDescribe' => $channelDescribe,
					'outputSourceType'=>3,//1 RTMP输出 2  HLS输出 3 RTMP 和HLS输出
					//'sourceList'=>array(array('name'=>'test','type'=>1))
                );
			$package['sourceList.1.name'] = 'test';
			$package['sourceList.1.type'] = 1;
			$a = $service->CreateLVBChannel($package);
			\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
			if($a == false){
				return $this->error("调用腾讯云接口错误：".$service->getError()->getMessage());
			}else{
				$channelId = $a['channel_id'];
				//创建成功 创建录播任务
				$param = array('channelId' => $channelId,
						'endTime' => '2030-12-31 12:00:00'
				);
				$b = $service->CreateRecord($param);
				if($a == false){
					return $this->error("调用腾讯云接口错误：".$service->getError()->getMessage());
				}else {
					$channelInfo = $a['channelInfo'];
					$upstream_address = $channelInfo['upstream_address'];
					$downstream_address = $channelInfo['downstream_address'][0]['hls_downstream_address'];
					$taskId = $b['task_id'];
					$channelDescribe = I("post.channelDescribe", NULL);
					$deviceDescribe = I("post.deviceDescribe", NULL);
					$manager = I("post.manager", NULL);
					$managerPhone = I("post.managerPhone", NULL);
					
					$postData = 'txyChannelId='.$channelId.'&channelName='.$channelName.'&taskId='.$taskId.'&channelDescribe='
							.$channelDescribe.'&deviceDescribe='.$deviceDescribe.'&manager='.$manager.'&status='.$status
							.'&managerPhone='.$managerPhone.'&pushUrl='.$upstream_address.'&pullUrl='.$downstream_address;
					$result = $this->curl_http(C('api_url').'channel/add?token='.$_SESSION["TOKEN"], $postData);
					if($result['Success']){
						$this->success("添加成功！", U("Admin/Channel/index"));
					}else{
						return $this->error($result['Message']);
					}
				}
			}
			
		}
	}
	
	function edit(){
		$channelId = I("get.channelId", 0);
		
		$result = $this->curl_http(C('video_url').'channel/get?token='.$_SESSION["TOKEN"], 'channelId='.$channelId);
		$this->assign($result['ReturnData']);
		$this->display();
	}
	
	function edit_post(){
		if(IS_POST){
			$channelName = I("post.channelName", '');
			if(empty($channelName)){
				return $this->error("机位名称不能为空！");
			}
			$channelId = I("post.channelId", 0);
			$status = I("post.status", 1);
			$manager = I("post.manager", NULL);
			$managerPhone = I("post.managerPhone", NULL);
			$channelDescribe = I("post.channelDescribe", NULL);
			$deviceDescribe = I("post.deviceDescribe", NULL);
			$postData = 'channelId='.$channelId.'&channelName='.$channelName.'&channelDescribe='.$channelDescribe
			           .'&deviceDescribe='.$deviceDescribe.'&manager='.$manager.'&status='.$status.'&managerPhone='.$managerPhone;
			$result = $this->curl_http(C('video_url').'channel/update?token='.$_SESSION["TOKEN"], $postData);
			if($result['Success']){
				$this->success("修改成功！", U("Admin/Channel/index"));
			}else{
				return $this->error($result['Message']);
			}
		}
	}
	
	function disable(){
		$channelId = I("get.channelId", 0);
		$txyChannelId = I("get.txyChannelId", 0);
		$status = 0;
		$config = array('SecretId'       => C("API_SecretId"),
				'SecretKey'      => C("API_SecretKey"),
				'RequestMethod'  => 'GET',
				'DefaultRegion'  => C("API_DefaultRegion"));
		
		vendor("QcloudApi.QcloudApi");
		$service = \QcloudApi::load(\QcloudApi::MODULE_LIVE, $config);
		$package = array('channelIds.1' => $txyChannelId);
		$a = $service->StopLVBChannel($package);
		\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
		if($a == false){
			return $this->error("调用腾讯云接口错误：".$service->getError()->getMessage());
		}else{
			$postData = 'channelId='.$channelId.'&status='.$status;
			$result = $this->curl_http(C('video_url').'channel/update?token='.$_SESSION["TOKEN"], $postData);
			if($result['Success']){
				$this->success("停用成功！", U("Admin/Channel/index"));
			}else{
				return $this->error($result['Message']);
			}
		}
	}
	
	function enable(){
		$channelId = I("get.channelId", 0);
		$txyChannelId = I("get.txyChannelId", 0);
		$status = 1;
		$config = array('SecretId'       => C("API_SecretId"),
				'SecretKey'      => C("API_SecretKey"),
				'RequestMethod'  => 'GET',
				'DefaultRegion'  => C("API_DefaultRegion"));
		
		vendor("QcloudApi.QcloudApi");
		$service = \QcloudApi::load(\QcloudApi::MODULE_LIVE, $config);
		$package = array('channelIds.1' => $txyChannelId);
		$a = $service->StartLVBChannel($package);
		\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
		if($a == false){
			return $this->error("调用腾讯云接口错误：".$service->getError()->getMessage());
		}else{
			$postData = 'channelId='.$channelId.'&status='.$status;
			$result = $this->curl_http(C('video_url').'channel/update?token='.$_SESSION["TOKEN"], $postData);
			if($result['Success']){
				$this->success("启用成功！", U("Admin/Channel/index"));
			}else{
				return $this->error($result['Message']);
			}
		}
	}
}